﻿// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================

using System.Collections.Generic;

namespace SL.IdentityModel.Claims
{

	/// <summary>
	/// This class contains staic properties for accesssing claim type constants
	/// </summary>
	public static class ClaimType
	{
		private const string nsWSIdentityClaims = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/";
		private const string nsClaims2008 = "http://schemas.microsoft.com/ws/2008/06/identity/claims/";
		private const string nsClaims2005 = "http://schemas.microsoft.com/ws/2005/05/identity/claims/";

		#region ClaimTypes

		public const string AuthenticationMethod = nsClaims2008 + "authenticationmethod";
		public const string Country = nsWSIdentityClaims + "country";
		public const string DateOfBirth = nsWSIdentityClaims + "dateofbirth";
		public const string Email = nsWSIdentityClaims + "emailaddress";
		public const string Gender = nsWSIdentityClaims + "gender";
		public const string GivenName = nsWSIdentityClaims + "givenname";
		public const string HomePhone = nsWSIdentityClaims + "homephone";
		public const string MobilePhone = nsWSIdentityClaims + "mobilephone";
		public const string Name = nsWSIdentityClaims + "name";
		public const string OtherPhone = nsWSIdentityClaims + "otherphone";
		public const string PPID = nsWSIdentityClaims + "privatepersonalidentifier";
		public const string PostalCode = nsWSIdentityClaims + "postalcode";
		public const string Role = nsClaims2008 + "role";
		public const string StateOrProvince = nsWSIdentityClaims + "stateorprovince";
		public const string StreetAddress = nsWSIdentityClaims + "streetaddress";
		public const string Surname = nsWSIdentityClaims + "surname";
		public const string UPN = nsClaims2005 + "upn";
		public const string WebPage = nsWSIdentityClaims + "webpage";

		#endregion

		private static Dictionary<string, string> _claimsDictionary = new Dictionary<string, string>();

		/// <summary>
		/// This field can be used to add name values pairs to the ClaimsDictionary.
		/// The name represents a friendly name for a claim type, example as 'Photo', while the value is
		/// the claim type uri, example 'http://my.claims/photo'.
		/// This dictionary is used when CreateFromKey() is called.
		/// </summary>
		public static Dictionary<string, string> ClaimsDictionay
		{
			get { return _claimsDictionary; }
		}

		/// <summary>
		/// creates a ClaimType class based on a key. First looks for a match in ClaimsDictionary, then looks at the list of 
		/// claims types that built in to ClaimType class. To look up builtin claim types, the field name of the claim type should be used
		/// </summary>
		/// <example>ClaimType.CreateFromKey("Email"); returns ClaimType.Email</example>
		/// <param name="key"></param>
		/// <returns></returns>
		public static string CreateFromKey(string key)
		{
			if (string.IsNullOrEmpty(key))
			{
				return null;
			}

			if (_claimsDictionary.ContainsKey(key))
			{
				return _claimsDictionary[key];
			}

			switch (key)
			{
				case "AuthenticationMethod":
					return ClaimType.AuthenticationMethod;
				case "Country":
					return ClaimType.Country;
				case "DateOfBirth":
					return ClaimType.DateOfBirth;
				case "Email":
					return ClaimType.Email;
				case "Gender":
					return ClaimType.Gender;
				case "GivenName":
					return ClaimType.GivenName;
				case "HomePhone":
					return ClaimType.HomePhone;
				case "MobilePhone":
					return ClaimType.MobilePhone;
				case "Name":
					return ClaimType.Name;
				case "OtherPhone":
					return ClaimType.OtherPhone;
				case "PPID":
					return ClaimType.PPID;
				case "Role":
					return ClaimType.Role;
				case "PostalCode":
					return ClaimType.PostalCode;
				case "StateOrProvince":
					return ClaimType.StateOrProvince;
				case "StreetAddress":
					return ClaimType.StreetAddress;
				case "Surname":
					return ClaimType.Surname;
				case "UPN":
					return ClaimType.UPN;
				case "WebPage":
					return ClaimType.WebPage;
				default:
					return null;
			}
		}
	}
}
